home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / raytrace / pov / gen / bstone / borlandc / t_cio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-01  |  3.4 KB  |  118 lines

  1. /************************************************************************/
  2. /**** My favourite IO routines                                       ****/
  3. /************************************************************************/
  4.  
  5.  
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include "t_cio.h"
  12.  
  13. #define T_CIOLEN 256
  14.  
  15. /**** Standard-error subroutine                                      ****/
  16. /**** exits with errnum                                              ****/
  17. /**** premsg: msg to show first, errnum:                             ****/
  18. /* 0: regular abort                                                     */
  19. /* 1: memory allocation error                                           */
  20. /* 2: io error                                                          */
  21. /* 3: number out of range                                               */
  22. /* 4: buffer overflow                                                   */
  23. /* 5: syntax error                                                      */
  24. void t_error(char *premsg,int errnum)
  25. {
  26.  fprintf(stderr,"\n");
  27.  if(*premsg) fprintf(stderr,"**** %s ****\n\n",premsg);
  28.  fprintf(stderr,"Program terminated - ");
  29.  switch(errnum)
  30.    {
  31.     case 0:   fprintf(stderr,"OK.");
  32.               break;
  33.     case 1:   fprintf(stderr,"Memory allocation error.");
  34.               break;
  35.     case 2:   fprintf(stderr,"In-Out error.");
  36.           break;
  37.     case 3:   fprintf(stderr,"Number out of range.");
  38.           break;
  39.     case 4:   fprintf(stderr,"Buffer overflow.");
  40.           break;
  41.     case 5:   fprintf(stderr,"Syntax error.");
  42.           break;
  43.     default:  fprintf(stderr,"Abort.");
  44.           break;
  45.    }
  46.  
  47.  fprintf(stderr,"\n\n");
  48.  exit(errnum);
  49.  return;
  50. }
  51.  
  52. static char t_path[T_CIOLEN]={"."};
  53.  
  54. /* get filename ************************************************/
  55. char *t_getfnam(void)
  56. {
  57.  char  tmpnam[T_CIOLEN];
  58.  char  *s;
  59.  
  60.  printf("Current directory path is: \"%s\"\n",t_path);
  61.  printf("Enter new directory path or press <RETURN>: ");
  62.  if(!(int)gets(tmpnam)) t_error("Can't get input line.",2);
  63.  if(sscanf(tmpnam,"*%s")!=EOF) strcpy(t_path,tmpnam);
  64.  s=t_path+strlen(t_path)+1;
  65.  printf("Enter file name: ");
  66.  while(fflush(stdin),!scanf("%s",s));
  67.  if((int)(s+strlen(s)-tmpnam)>=T_CIOLEN) t_error("Path too long.",4);
  68.  return(s);
  69. }
  70.  
  71. /**** opens file, if name==NULL -> asks for file name                 ****/
  72. /**** t_path must be set                                              ****/
  73. /**** if using UNIX etc, change backslashes in function body          ****/
  74. FILE *t_fopen(char *name, char *mode)
  75. {
  76.  char *s;
  77.  FILE *fep;
  78.  
  79.  s=t_path;
  80.  if(name==(char *)NULL) { t_getfnam(); s+=strlen(t_path); }
  81.  else strcpy((s+=strlen(t_path))+1,name);
  82.  
  83.  *s='\\';
  84.  if((fep=fopen(t_path,mode))==(FILE *)NULL)
  85.    {
  86.     strcat(t_path," could not be opened for ");
  87.     strcat(t_path,mode);
  88.     t_error(t_path,2);
  89.    }
  90.  *s=(char)0;
  91.  return(fep);
  92. }
  93.  
  94. /**** Menus                                                       ****/
  95. /**** menu[]={"first item","second item",""}                      ****/
  96. /**** returns number of selected item                             ****/
  97. int ar_menu(char *menu[])
  98. {
  99.  int i,c,p,anz;
  100.  
  101.  for(i=0;*menu[i] && i<26;i++)
  102.    {
  103.     printf("%c: %s\n",(char)i+'a',menu[i]);
  104.    }
  105.  anz=i;
  106.  printf("Select: ");
  107.  do
  108.    {
  109.     c=getchar(); c=tolower(c); p=c-'a';
  110.    }
  111.  while(!islower(c) && p<anz && p>=0);
  112.  printf("%s\n\n\n",menu[p]);
  113.  return(p);
  114. }
  115.  
  116.  
  117.  
  118.